﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DIProject.Data
{
    public class Links
    {
        /// <summary>
        /// Вставляем новую перекодировку.
        /// </summary>
        /// <param name="goodsId">наш идентификатор</param>
        /// <param name="contragentId">идентификатор контрагента</param>
        /// <param name="goodsCode">код товора контрагента</param>
        /// <param name="goodsName">наименование товара контрагента</param>
        /// <param name="countryName">наименование страны контрагента</param>
        /// <param name="manufacturerName">наименование производителя контрагента</param>
        /// <param name="note">описание</param>
        /// <returns>вставленная перекодировка</returns>
        public static DataSets.waybillsData.GoodsVsContragentsInsertDataTable AddNewLink
            (
            Int32? goodsId,
            Int32? contragentId,
            Int32? goodsCode,
            string goodsName,
            string countryName,
            string manufacturerName,
            string note
            )
        {
           /* Int32? GoodsId;
            Int32? ContragentId;
            Int32? GoodsCode;
            string GoodsName;
            string CountryName;
            string ManufacturerName;
            string Note;*/
            DateTime MD = DateTime.Now;
            string MW = FirstForm.Login;
            try
            {
                DataSets.waybillsDataTableAdapters.GoodsVsContragentsInsertTableAdapter adapter = new DIProject.DataSets.waybillsDataTableAdapters.GoodsVsContragentsInsertTableAdapter();
                DataSets.waybillsData.GoodsVsContragentsInsertDataTable result = null;
                result = adapter.GetData(goodsId, contragentId, goodsCode, goodsName, countryName, manufacturerName, note, MD, MW);
                return result;
            }
            catch (System.Data.SqlClient.SqlException se)
            {
                throw new Util.DIException("Ошибка при добавлении новой перекодировки", se);
            }
            catch (System.Exception e)
            {
                throw new Util.DIException("Ошибка при добавлении новой перекодировки", e);
            }
        }
        public static void DeleteLink(Int32 linkId, DateTime mD)
        {
            string conStr = Properties.Settings.Default.DIAptekaConnectionString;
            string QueryString = @"dbo.GoodsVsContragentsDelete";
            using (SqlConnection connection = new SqlConnection(conStr))
            {
                try
                {
                    SqlCommand command = new SqlCommand(QueryString, connection);
                    command.CommandType = CommandType.StoredProcedure;
                    SqlParameter Original_Id = new SqlParameter("@Original_Id", SqlDbType.Int);
                    Original_Id.Value = linkId;
                    command.Parameters.Add(Original_Id);
                    SqlParameter Original_MD = new SqlParameter("@Original_MD", SqlDbType.DateTime);
                    Original_MD.Value = mD;
                    SqlParameter md = new SqlParameter("@MW", SqlDbType.VarChar, 50);
                    md.Value = FirstForm.Login;
                    command.Parameters.Add(md);
                    command.Parameters.Add(Original_MD);
                    connection.Open();
                    command.ExecuteNonQuery();
                }
                catch (SqlException se)
                {
                    System.Windows.Forms.MessageBox.Show("Невозможно удалить перекодировку\n" + se.Message, "Ошибка", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    throw new DIProject.Util.DIException("",se);
                }
                catch (Exception ee)
                {
                    System.Windows.Forms.MessageBox.Show("Невозможно удалить перекодировку\n" + ee.Message, "Ошибка", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    throw new DIProject.Util.DIException("",ee);
                }
            }
        }
        public static void UpdateLink(Int32 goodsId, Int32 contragentId, Int32 goodsCode, string goodsName,
            string countryName, string manufacturerName, string note, DateTime mD, string mW, bool toDelete,
            Int32 original_Id, DateTime original_MD)
        {
            string QueryString = @"dbo.GoodsVsContragentsUpdateEx";
            string ConnectionString = Properties.Settings.Default.DIAptekaConnectionString;
            try
            {
                using (SqlConnection connection = new SqlConnection(ConnectionString))
                {
                    SqlCommand command = new SqlCommand(QueryString, connection);
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@GoodsId", goodsId);
                    command.Parameters.AddWithValue("@ContragentId", contragentId);
                    command.Parameters.AddWithValue("@GoodsCode", goodsCode);
                    if (string.IsNullOrEmpty(goodsName))
                        throw new Exception("Наименование товара не может быть пустым");
                    command.Parameters.AddWithValue("@GoodsName", goodsName);
                    command.Parameters.AddWithValue("@MD", mD);
                    command.Parameters.AddWithValue("@MW", mW);
                    command.Parameters.AddWithValue("@Original_Id", original_Id);
                    command.Parameters.AddWithValue("@Original_MD", original_MD);
                    command.Parameters.AddWithValue("@ToDelete", toDelete);
                    SqlParameter p = command.Parameters.Add("@ManufacturerName", SqlDbType.VarChar, 70);
                    if (string.IsNullOrEmpty(manufacturerName))
                    {
                        p.Value = DBNull.Value;
                    }
                    else
                    {
                        p.Value = manufacturerName;
                    }
                    p = command.Parameters.Add("@CountryName", SqlDbType.VarChar, 20);
                    if (string.IsNullOrEmpty(countryName))
                    {
                        p.Value = DBNull.Value;
                    }
                    else
                    {
                        p.Value = countryName;
                    }
                    p = command.Parameters.Add("@Note", SqlDbType.VarChar, 50);
                    if (string.IsNullOrEmpty(note))
                    {
                        p.Value = DBNull.Value;
                    }
                    else
                    {
                        p.Value = note;
                    }
                    connection.Open();
                    command.ExecuteNonQuery();
                }
            }
            catch (SqlException se)
            {
                string s = se.ToString();
                throw se;
            }
            catch (Exception ee)
            {
                string s = ee.ToString();
                throw ee;
            }
        }
    }
}
